from textblob import TextBlob
from textblob.classifiers import NaiveBayesClassifier

train_data = [
    ('I love this sandwich.', 'pos'),
    ('This is an amazing place!', 'pos'),
    ('I feel very good about these beers.', 'pos'),
    ('This is my best work.', 'pos'),
    ("What an awesome view", 'pos'),
    ('I do not like this restaurant', 'neg'),
    ('I am tired of this stuff.', 'neg'),
    ("I can't deal with this", 'neg'),
    ('He is my sworn enemy!', 'neg'),
    ('My boss is horrible.', 'neg')
]

test_data = [
    ('The beer was good.', 'pos'),
    ('I do not enjoy my job', 'neg'),
    ("I ain't feeling dandy today.", 'neg'),
    ("I feel amazing!", 'pos'),
    ('Gary is a friend of mine.', 'pos'),
    ("I can't believe I'm doing this.", 'neg')
]

# 创建并训练分类器
cl = NaiveBayesClassifier(train_data)

# 测试分类器
print(cl.classify("This is an amazing library!"))  # 输出: pos
print(cl.accuracy(test_data))  # 计算准确率

# 显示最有信息量的特征
cl.show_informative_features(5)